﻿using System;
using System.Drawing;

namespace ImgToTxt
{
    public class Captcha
    {
        #region 字符阵列
        public static String[][] CodeNum =
        {
            new String[]{
                "                 ",
                "00001111111110000",
                "00111111111111100",
                "01111000000001110",
                "11100000000000111",
                "11000000000000011",
                "11000000000000011",
                "11000000000000011",
                "11100000000000111",
                "01111000000011110",
                "00111111111111100",
                "00001111111110000",
                "                 ",
            },
            new String[]{
                "                 ",
                "01100000000000000",
                "01100000000000011",
                "11100000000000011",
                "11000000000000011",
                "11000000000000011",
                "11111111111111111",
                "11111111111111111",
                "00000000000000011",
                "00000000000000011",
                "00000000000000011",
                "00000000000000011",
                "                 ",
            },
            new String[]{
                "                 ",
                "01100000000000111",
                "01100000000001111",
                "11000000000011111",
                "11000000000111011",
                "11000000001110011",
                "11000000011100011",
                "11000000111000011",
                "01100001110000011",
                "01111111100000011",
                "00011110000000011",
                "                 ",
                "                 ",
            },
            new String[]{
                "00000000000000110",
                "01100000000000110",
                "11000000000000011",
                "11000001100000011",
                "11000001100000011",
                "11000001100000011",
                "11000001100000011",
                "11000001100000011",
                "01100011110000111",
                "01111110110000110",
                "00111100011111100",
                "00000000001111000",
                "                 ",
            },
            new String[]{
                "00000000000110000",
                "00000000011110000",
                "00000000111110000",
                "00000011100110000",
                "00001111000110000",
                "00011100000110000",
                "01110000000110000",
                "11100000000110000",
                "11111111111111111",
                "11111111111111111",
                "00000000000110000",
                "00000000000110000",
                "00000000000110000",
            },
            new String[]{
                "00000000000000110",
                "11111111000000111",
                "11111110000000011",
                "11000110000000011",
                "11000110000000011",
                "11000110000000011",
                "11000110000000011",
                "11000111000000111",
                "11000011100001110",
                "11000001111111100",
                "00000000111111000",
                "                 ",
                "                 ",
            },
            new String[]{
                "                 ",
                "00000111111110000",
                "00011111111111100",
                "00110001110011110",
                "01100011000000111",
                "11000110000000011",
                "11000110000000011",
                "11000110000000011",
                "11000111000000111",
                "11000011100001110",
                "01000001111111100",
                "00000000111111000",
                "                 ",
            },
            new String[]{
                "                 ",
                "11000000000000000",
                "11000000000000000",
                "11000000000000000",
                "11000000000000111",
                "11000000000111111",
                "11000000011111100",
                "11000011111110000",
                "11001111110000000",
                "11111110000000000",
                "11111000000000000",
                "11000000000000000",
                "                 ",
            },
            new String[]{
                "                 ",
                "00000000001111100",
                "00111100011111110",
                "01111110110000110",
                "11100011100000011",
                "11000001100000011",
                "11000001100000011",
                "11000001100000011",
                "11000001100000011",
                "11100011110000110",
                "01111110011111110",
                "00111100001111000",
                "                 ",
            },
            new String[]{
                "00011111000000000",
                "01111111110000110",
                "01110000110000011",
                "11100000011000011",
                "11000000011000011",
                "11000000011000011",
                "11000000011000011",
                "11100000011000110",
                "01100000110001100",
                "00111111111111000",
                "00001111111100000",
                "                 ",
                "                 ",
            },
            new String[]{
                "               ",
                "111111111111111",
                "111111111111111",
                "100000000000010",
                "100000000000010",
                "100000000000010",
                "100000000000010",
                "111111111111111",
                "111111111111111",
                "               ",
                "               ",
                "               ",
                "               ",
            }
        };
        #endregion

        /// <summary>
        /// 位图转字符矩阵(纵向输出)
        /// </summary>
        /// <param name="SrcBmp"></param>
        /// <returns></returns>
        public static string[] BmpToStringH(Bitmap SrcBmp)
        {
            string[] SrcPixel = new string[SrcBmp.Width];
            int height = SrcBmp.Height;
            Color tmpColor = new Color();
            for (int x = 0; x < SrcPixel.Length; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    tmpColor = SrcBmp.GetPixel(x, y);
                    if (tmpColor.R > 60 || tmpColor.G > 60 || tmpColor.B > 60)
                    {
                        SrcPixel[x] += 0;
                    }
                    else
                    {
                        SrcPixel[x] += 1;
                    }
                }
            }
            return SrcPixel;
        }
        /// <summary>
        /// 位图转字符矩阵(横向输出)
        /// </summary>
        /// <param name="SrcBmp"></param>
        /// <returns></returns>
        public static string[] BmpToStringW(Bitmap SrcBmp)
        {
            string[] SrcPixel = new string[SrcBmp.Height];
            int width = SrcBmp.Width;
            Color tmpColor = new Color();
            for (int y = 0; y < SrcPixel.Length; y++)
            {
                for (int x = 0; x< width; x++)
                {
                    tmpColor = SrcBmp.GetPixel(x, y);
                    if (tmpColor.R > 60 || tmpColor.G > 60 || tmpColor.B > 60)
                    {
                        SrcPixel[y] += 0;
                    }
                    else
                    {
                        SrcPixel[y] += 1;
                    }
                }
            }
            return SrcPixel;
        }
        /// <summary>
        /// 位图转数字矩阵
        /// </summary>
        /// <param name="SrcBmp"></param>
        /// <returns></returns>
        public static int[,] BmpToInt(Bitmap SrcBmp)
        {
            int[,] SrcPixel = new int[SrcBmp.Width, SrcBmp.Height];
            int width = SrcBmp.Width;
            int height = SrcBmp.Height;
            Color tmpColor = new Color();
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    tmpColor = SrcBmp.GetPixel(x, y);
                    if (tmpColor.R > 60 || tmpColor.G > 60 || tmpColor.B > 60)
                    {
                        SrcPixel[x, y] = 0;
                    }
                    else
                    {
                        SrcPixel[x, y] = 1;
                    }
                }
            }
            return SrcPixel;
        }

        /// <summary>
        /// 位图黑白化处理
        /// </summary>
        /// <param name="SrcImg">24bit位图</param>
        /// <param name="color">参考颜色</param>
        /// <returns>单色位图</returns>
        public static Bitmap Get1bppBMP(Bitmap SrcImg, Color color)
        {
            int width = SrcImg.Width;
            int height = SrcImg.Height;
            Color tmpColor = new Color();
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    tmpColor = SrcImg.GetPixel(x, y);
                    if (tmpColor.R > color.R || tmpColor.G > color.G || tmpColor.B > color.B)
                    {
                        SrcImg.SetPixel(x, y, Color.FromArgb(255, 255, 255));
                    }
                    else
                    {
                        SrcImg.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                    }
                }
            }
            return SrcImg;
        }
    }
}
